package com.yvdedu.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Service;

import com.yvdedu.entity.BookTable;
import com.yvdedu.entity.User;
import com.yvdedu.factory.MySession;
import com.yvdedu.implement.BookTbale_Mgr_Imp;

@Service
public class BookTable_Mgr_Service implements BookTbale_Mgr_Imp {

	private Session session;
	private Transaction tc;
	
	public String save(String BookName, String disposableType, String joinTime, String exitTime, String dateType,
			String integral, String remarks) {
		session= MySession.getSession();
		BookTable cu = new BookTable();
		cu.setTableName(BookName);
		cu.setDisposable(disposableType);
		cu.setJoinTime(joinTime);
		cu.setExitTime(exitTime);
		cu.setWorkingDay(dateType);
		cu.setIntegral(integral);
		cu.setRemarks(remarks);
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(d);
		cu.setDateCreated(date);
		try {
			tc = session.beginTransaction();
			session.save(cu);
			tc.commit();
			return "ok";
		} catch (Exception e) {
			tc.rollback();
			return "defeated";
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<BookTable> queryweek(String week) {
		session= MySession.getSession();
		List<BookTable> tableList =null;
			try {
				if (week == null || "全部".equals(week) || "".equals(week)) {
						tableList = (List<BookTable>) session.createQuery("from BookTable").list();
						System.out.println(tableList);
						return tableList;
					}else{
						tableList = (List<BookTable>) session.createQuery("from BookTable where workingDay= ?").setString(0, week).list();
						System.out.println(tableList);
						return tableList;
					}
			} catch (Exception e) {
				return null;
			} finally {
				session.close();
				
			}
	}

	@SuppressWarnings("unchecked")
	public List<BookTable> queryday(String dateCreated) {
		session= MySession.getSession();
		List<BookTable> tableList =null;
		System.out.println("ccc"+dateCreated);
			try {
				if (dateCreated == null || "年-月-日".equals(dateCreated) || "".equals(dateCreated)) {
					tableList = (List<BookTable>) session.createQuery("from BookTable").list();
					System.out.println(tableList);
					return tableList;
				}else{
					tableList = (List<BookTable>) session
							.createQuery("from BookTable where dateCreated= ? ").setString(0, dateCreated).list();
					System.out.println(tableList);
					return tableList;
				}
			} catch (Exception e) {
				return null;
			} finally {
				session.close();
			}
	}

	public String delete(String delete, String courseId) {
		session= MySession.getSession();
		if ("1".equals(delete)) {
			try {
				tc = session.beginTransaction();

				session.createQuery("update Course c set c.unseal = 'false' where id = ?").setString(0, courseId)
						.executeUpdate();
				tc.commit();
				return "ok";
			} catch (Exception e) {
				tc.rollback();
				return "defeated";
			} finally {
				session.close();
			}
		} else {
			System.out.println("娌掗伕鎿囩⒑瀹�");
			return "defeated";
		}

	}
}
